package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmplyeeService;

import com.sun.org.apache.regexp.internal.RE;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@RestController
//配置以及路径
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmplyeeService emplyeeService;
    //参数有三种：Json格式的参数 {key:value,key:value}
    @RequestMapping("/login")
    public R<Employee> login(@RequestBody Employee empolyee, HttpSession session){
        String password = empolyee.getPassword();
        String md5password = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,empolyee.getUsername());
        Employee one = emplyeeService.getOne(queryWrapper);
        if(one==null){
            return R.error("用户不存在");
        }
        if(!one.getPassword().equals(md5password)){
            return R.error("密码错误");
        }
        if(one.getStatus() == 0){
            return R.error("账号已禁用");
        }
        session.setAttribute("employee",one.getId());
        return R.success(one);

    }

    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
        //清除session中保存的内容
//        session.removeAttribute("employee");
        //退出得特别干净：密码，用户名，手机号
        session.invalidate();
        return R.success("退出成功");
    }
    /**
     * 新增员工
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Employee employee,HttpSession session){
        log.info("新增员工，员工信息：{}",employee.toString());

        //设置初始密码123456，需要进行md5加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        //获得当前登录用户的id
        Long empId = (Long) session.getAttribute("employee");

        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);

        emplyeeService.save(employee);
        return R.success("新增员工成功");
    }
    /**
     * 员工信息分页查询
     * @param page 当前查询页码
     * @param pageSize 每页展示记录数
     * @param name 员工姓名 - 可选参数
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        log.info("page = {},pageSize = {},name = {}" ,page,pageSize,name);
        //构造分页构造器
        Page pageInfo = new Page(page,pageSize);

        //构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        //执行查询
        emplyeeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }
    /**
     * 根据id修改员工信息
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        log.info("修改员工.....");
        if (employee.getId()==1){
            return R.error("管理员账号不能修改");
        }

        Long empId = (Long)request.getSession().getAttribute("employee");

        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(empId);
        emplyeeService.updateById(employee);

        return R.success("员工信息修改成功");
    }
}